把python 数组,量化为从1开始的数组

问题描述:

比方我有一个数组:a=[10,12,3,4,9]

我最终想要的是:[4,5,1,2,3]

就是按照大小将数组a中的元素转换为其对应的大小排序值。


思路:1,先用字典保存位置和值

    2,按照值对字典进行排序

       3,对排序后的值进行量化:量化为:1,2,3,4,5

    4,在对tuple按照位置进行排序

    5,导出排序后的值即可

dict={}
for i in xrange(len(FINAL_STATISTICS)):
    dict.setdefault(i,FINAL_STATISTICS[i])
sorted_tuple=sorted(dict.iteritems(),key=lambda dict:dict[1]) #对字典按照key值进行排序

temp_list=[]
temp_count=1
for i in xrange(len(sorted_tuple)):
    if i==0:
        temp_list.append([sorted_tuple[i][0],temp_count])
            #temp_count+=1
        continue
    if sorted_tuple[i][1]==sorted_tuple[i-1][1] :
        temp_list.append([sorted_tuple[i][0],temp_count])
    else :
        temp_count+=1<span style="white-space:pre">					</span>#对tuple的值进行量化
        temp_list.append([sorted_tuple[i][0],temp_count])
        
        
temp_list  =sorted(temp_list)

final_sorted=[]
for item in temp_list:
    final_sorted.append(item[1])      #输出tuple的第二个元素
print final_sorted


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值